<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxw/model')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class fuwu extends \plugin{
	public function add($postdata,$storeid=0,$noexi=false,$canadd=false){
		if(!$postdata['day']){
			if($noexi){
				return false;
			}
			exi('请填写上课日期！','error');
		}
		$data=array(
			'day'=>trim($postdata['day']),
			'kejianid'=>trim($postdata['kejianid']),
			'workerid'=>trim($postdata['workerid']),
			'daytime'=>strtotime($postdata['day']),
			'status'=>intval($postdata['status']),
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_yxw_class_kc',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['storeid']=$_SESSION['storeid'];
			$data['classid']=$postdata['classid'];
			$data['fuwuid']=$postdata['fuwuid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_yxw_class_kc',$data);
		}
		return $id;
	}
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传CSV文件！','error');
		}
		if(!$post['classid']){
			exi('请先由售后群进入！','error');
		}
		if(!$post['fuwuid']){
			exi('请先售后群--使用进入！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel($localexcel);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['上课日期'] && !$res[0]['课件名称']){
			$excel=cfc('tools')->importExcel($localexcel,true);
			$res=$this->model('model')->get_csvitems($excel);
			if(!$res[0]['上课日期'] && !$res[0]['课件名称']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$cdata=$this->csv_add($res,$post['classid'],$post['fuwuid']);
		if($cdata){
			cache_write('kcdata',$cdata,'jy_yxw');
			exi('导入成功（【上课日期】重复的会自动忽略）！','','/shouhou.cdata/');
		}else{
			exi('导入成功！','','/shouhou.class_kc/');
		}
	}
	public function csv_add($rows,$classid=0,$fuwuid=0){
		$cdata=array();
		foreach($rows as $item){
			if(!$item['上课日期']){
				//exi('有数据不存在上课日期！','error');
			}
			$item['上课日期']=trim($item['上课日期']);
			$item['课件名称']=trim($item['课件名称']);
			$item['售后技术姓名']=trim($item['售后技术姓名']);
			$daytime=strtotime($item['上课日期']);
			$data=array(
				'day'=>date('Y-m-d',$daytime),
				'daytime'=>$daytime,
				'fuwuid'=>$fuwuid,
				'classid'=>$classid,
			);
			if($item['课件名称']){
				$data['kejianid']=pdo_getcolumn('jy_yxw_kejian',array('fuwuid'=>$fuwuid,'uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['课件名称'])),'id');
				if(!$data['kejianid']){
					$data=array(
						'title'=>trim($item['课件名称']),
						'fuwuid'=>$fuwuid,
						'status'=>1,
					);
					$data['uniacid']=$_SESSION['uniacid'];
					$data['storeid']=$_SESSION['storeid'];
					$data['createtime']=TIMESTAMP;
					pdo_insert('jy_yxw_kejian',$data);
					$data['kejianid']=pdo_insertid();
				}
			}
			if($item['售后技术姓名']){
				$data['workerid']=pdo_getcolumn('jy_yxw_worker',array('juese'=>10,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['售后技术姓名'])),'id');
			}
			//上课日期重复
			$isadd=pdo_get('jy_yxw_class_kc',array('day'=>$data['day'],'uniacid'=>$_SESSION['uniacid'],'classid'=>$data['classid'],'fuwuid'=>$data['fuwuid']),array('id'));
			if($isadd['id']){
				$cdata[]=array('day'=>$data['day'],'kejianid'=>$data['kejianid'],'workerid'=>$data['workerid']);
				continue;
			}
			$this->add($data,0,true,true);
		}
		return $cdata;
	}
}
?>